﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace CarInfor.UI
{
    public partial class frmBackup : Form
    {
        public frmBackup()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 备份
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBackup_Click(object sender, EventArgs e)
        {

            SaveFileDialog savefile = new SaveFileDialog();
            savefile.Filter = "Access数据文件(*.mdb)|*.mdb";
            savefile.DefaultExt = ".mdb";
            savefile.FileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".mdb";
            savefile.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            if (savefile.ShowDialog() == DialogResult.OK)
            {

                Backup(Application.StartupPath + @"\db.mdb", savefile.FileName);
            }
        }

        /// <summary>
        /// 还原
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRestore_Click(object sender, EventArgs e)
        {
            OpenFileDialog openfile = new OpenFileDialog();
            openfile.Filter = "Access数据文件(*.mdb)|*.mdb";
            openfile.DefaultExt = ".mdb";
            openfile.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            if (openfile.ShowDialog() == DialogResult.OK)
            {
                if (MessageHelper.ShowOKCancelChoice("该操作将覆盖现有数据，确定还原？") == DialogResult.OK)
                {
                    Recover(openfile.FileName, Application.StartupPath + @"\db.mdb");
                }
            }
        }

        /// <summary>
        /// 文件备份
        /// </summary>
        /// <param name="mdb1"></param>
        /// <param name="mdb2"></param>

        public void Backup(string mdb1, string mdb2)
        {
            if (!File.Exists(mdb1))
            {
                MessageHelper.ShowInfo("源数据库不存在，无法备份");
            }
            try
            {
                File.Copy(mdb1, mdb2, true);
                MessageHelper.ShowInfo("备份成功！");
            }
            catch (IOException ex)
            {
                MessageHelper.ShowFail("备份失败！" + ex.Message);
            }
        }


        /// <summary>
        /// 文件还原
        /// </summary>
        /// <param name="mdb1"></param>
        /// <param name="mdb2"></param>

        public void Recover(string mdb1, string mdb2)
        {
            if (!File.Exists(mdb1))
            {
                MessageHelper.ShowInfo("源数据库不存在，无法还原");
            }
            try
            {
                File.Copy(mdb1, mdb2, true);
                MessageHelper.ShowInfo("还原成功！");
            }
            catch (IOException ex)
            {
                MessageHelper.ShowFail("还原失败！" + ex.Message);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}
